﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
using System.IO.Ports;

namespace SCI_actuator
{
    public partial class GroupMovement : Form
    {   
        private menu_control frmain;
        private GroupCommand group_command;
        private bool flag_ready1;
        private bool flag_ready2;
        private bool flag_ready3;
        private bool flag_ready4;
        private int action2_count;
        private int action4_count;

        private System.Timers.Timer action1;
        private System.Timers.Timer action2;
        private System.Timers.Timer action3;
        private System.Timers.Timer action4;


        public GroupMovement(menu_control fr)
        {
            InitializeComponent();

            this.frmain = fr;
            this.group_command = new GroupCommand(fr);

            this.flag_ready1 = false;
            this.buttonStart1.Text = "开始";
            this.buttonStart1.Enabled = false;

            this.flag_ready2 = false;
            this.buttonStart2.Text = "开始";
            this.buttonStart2.Enabled = false;
            action2_count = 0;

            this.flag_ready3 = false;
            this.buttonStart3.Text = "开始";
            this.buttonStart3.Enabled = false;

            this.flag_ready4 = false;
            this.buttonStart4.Text = "开始";
            this.buttonStart4.Enabled = false;
            action4_count = 0;

            action1 = new System.Timers.Timer();
            action1.Interval = 250;
            action1.Enabled = Enabled;
            action1.Stop();
            action1.Elapsed += new ElapsedEventHandler(action1_tick);

            action2 = new System.Timers.Timer();
            action2.Interval = 250;
            action2.Enabled = Enabled;
            action2.Stop();
            action2.Elapsed += new ElapsedEventHandler(action2_tick);

            action3 = new System.Timers.Timer();
            action3.Interval = 360;
            action3.Enabled = Enabled;
            action3.Stop();
            action3.Elapsed += new ElapsedEventHandler(action3_tick);

            action4 = new System.Timers.Timer();
            action4.Interval = 280;
            action4.Enabled = Enabled;
            action4.Stop();
            action4.Elapsed += new ElapsedEventHandler(action4_tick);
        }

        private void Delay_ms(int mm)
        {
            DateTime current = DateTime.Now;

            while (current.AddMilliseconds(mm) > DateTime.Now)
            {
                Application.DoEvents();
            }
            return;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        private void buttonReady1_Click(object sender, EventArgs e)
        {
            group_command.Start_T("01", "C4", "00", "10", "02", "2C", "00", "10", "02", "2C", "00", "10", "02", "2C", "00", "10");
            
            flag_ready1 = true;
            buttonStart1.Enabled = true;
            buttonReady2.Enabled = false;
            buttonStart2.Enabled = false;
            buttonReady3.Enabled = false;
            buttonStart3.Enabled = false;
            buttonReady4.Enabled = false;
            buttonStart4.Enabled = false;
            buttonStart1.Text = "开始";
            buttonStart2.Text = "开始";
            buttonStart3.Text = "开始";
            buttonStart4.Text = "开始";
            //清除节点  00是位置时间  01是连续轨迹
            group_command.ClearBuffer("02", "00");
            group_command.ClearBuffer("03", "00");
            group_command.ClearBuffer("04", "00");
        }

        private void buttonStart1_Click(object sender, EventArgs e)
        {
            if (flag_ready1)
            {
                if (buttonStart1.Text == "开始" || buttonStart1.Text == "继续")   //显示为“开始”   开始运动
                {
                    if (buttonStart1.Text == "开始")
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 0;
                    }

                    buttonStart1.Text = "停止";
                    buttonReady1.Enabled = false;
                    buttonReady2.Enabled = false;
                    buttonReady3.Enabled = false;
                    buttonReady4.Enabled = false;

                    group_command.Move_Curve();
                    action1.Start();
                  
                }
                else if(buttonStart1.Text == "停止")           //显示为“停止”    停止运动
                {
                    //group_command.Break();
                    buttonStart1.Text = "继续";
                    buttonReady1.Enabled = true;
                    buttonReady2.Enabled = true;
                    buttonReady3.Enabled = true;
                    buttonReady4.Enabled = true;

                    action1.Stop();

                }
            }
        }

        private void action1_tick(object sender, ElapsedEventArgs e)
        {
            if (this.frmain.group_counter_arm == 0)
            {
                this.frmain.group_counter_arm = 1;
            }
            else if (this.frmain.group_counter_arm == 1)
            {
                if (this.frmain.unstop_counter_arm == 0)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_Curve("01", "C7", "03", "00", "01", "83", "03", "00", "02", "D4", "03", "00");
                }
                else if (this.frmain.unstop_counter_arm == 1)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                }
                else if (this.frmain.unstop_counter_arm == 2)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_Curve("02", "90", "03", "00", "02", "D4", "03", "00", "01", "83", "03", "00");
                }
                else if (this.frmain.unstop_counter_arm == 3)
                {
                    this.frmain.unstop_counter_arm = 0;
                    this.frmain.group_counter_arm = 2;
                    group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                }
            }
            else if (this.frmain.group_counter_arm == 2)
            {
                this.frmain.group_counter_arm = 3;
                group_command.Move_Curve();
            }
            else if (this.frmain.group_counter_arm == 3)
            {
                this.frmain.group_counter_arm = 3;
                group_command.BufferLeft_Curve("02");
            }

        }

        private void buttonReady2_Click(object sender, EventArgs e)
        {
            group_command.Start_T("01", "C4", "00", "10", "02", "2C", "00", "10", "02", "2C", "00", "10", "02", "2C", "00", "10");

            flag_ready2 = true;
            buttonStart2.Enabled = true;
            buttonReady1.Enabled = false;
            buttonStart1.Enabled = false;
            buttonReady3.Enabled = false;
            buttonStart3.Enabled = false;
            buttonReady4.Enabled = false;
            buttonStart4.Enabled = false;
            buttonStart2.Text = "开始";
            buttonStart1.Text = "开始";
            buttonStart3.Text = "开始";
            buttonStart4.Text = "开始";
            //清除节点  00是位置时间  01是连续轨迹
            group_command.ClearBuffer("02", "00");
            group_command.ClearBuffer("03", "00");
            group_command.ClearBuffer("04", "00");
        }

        private void buttonStart2_Click(object sender, EventArgs e)
        {
            if (flag_ready2)
            {
                if (buttonStart2.Text == "开始" || buttonStart2.Text == "继续")   //显示为“开始”   开始运动
                {
                    if (buttonStart2.Text == "开始")
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 0;
                    }

                    buttonStart2.Text = "停止";
                    buttonReady2.Enabled = false;
                    buttonReady1.Enabled = false;
                    buttonReady3.Enabled = false;
                    buttonReady4.Enabled = false;

                    group_command.Move_Curve();
                    action2.Start();
                }
                else if (buttonStart2.Text == "停止")           //显示为“停止”    停止运动
                {
                    //group_command.Break();
                    buttonStart2.Text = "继续";
                    buttonReady2.Enabled = true;
                    buttonReady1.Enabled = true;
                    buttonReady3.Enabled = true;
                    buttonReady4.Enabled = true;
                    
                    action2.Stop();
                }
            }
        }

        private void action2_tick(object sender, ElapsedEventArgs e)
        {
            if (this.frmain.group_counter_arm == 0)
            {
                this.frmain.group_counter_arm = 1;
            }
            else if (this.frmain.group_counter_arm == 1)
            {
                if (action2_count == 0)
                {
                    if (this.frmain.unstop_counter_arm == 0)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("01", "C7", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 1)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("02", "90", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 2)
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 2;
                        action2_count = 1;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                    }
                }
                else if (action2_count == 1)
                {
                    if (this.frmain.unstop_counter_arm == 0)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "01", "83", "03", "00", "02", "2C", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 1)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "D4", "03", "00", "02", "2C", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 2)
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 2;
                        action2_count = 2;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                    }
                }
                else if (action2_count == 2)
                {
                    if (this.frmain.unstop_counter_arm == 0)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "D4", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 1)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "01", "83", "03", "00");
                    }
                    else if (this.frmain.unstop_counter_arm == 2)
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 2;
                        action2_count = 0;
                        group_command.AddPoint_Curve("02", "2C", "03", "00", "02", "2C", "03", "00", "02", "2C", "03", "00");
                    }
                }
            }
            else if (this.frmain.group_counter_arm == 2)
            {
                this.frmain.group_counter_arm = 3;
                group_command.Move_Curve();
            }
            else if (this.frmain.group_counter_arm == 3)
            {
                this.frmain.group_counter_arm = 3;
                group_command.BufferLeft_Curve("02");
            }
        }

        private void buttonReady3_Click(object sender, EventArgs e)
        {
            group_command.Start_T("01", "C4", "00", "20", "01", "83", "00", "20", "01", "83", "00", "20", "02", "D5", "00", "20");

            flag_ready3 = true;
            buttonStart3.Enabled = true;
            buttonReady1.Enabled = false;
            buttonStart1.Enabled = false;
            buttonReady2.Enabled = false;
            buttonStart2.Enabled = false;
            buttonReady4.Enabled = false;
            buttonStart4.Enabled = false;
            buttonStart3.Text = "开始";
            buttonStart2.Text = "开始";
            buttonStart1.Text = "开始";
            buttonStart4.Text = "开始";
            //清除节点  00是位置时间  01是连续轨迹
            group_command.ClearBuffer("02", "00");
            group_command.ClearBuffer("03", "00");
            group_command.ClearBuffer("04", "00");
        }

        private void buttonStart3_Click(object sender, EventArgs e)
        {
            if (flag_ready3)
            {
                if (buttonStart3.Text == "开始" || buttonStart3.Text == "继续")   //显示为“开始”   开始运动
                {
                    if (buttonStart3.Text == "开始")
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 0;
                    }

                    buttonStart3.Text = "停止";
                    buttonReady3.Enabled = false;
                    buttonReady1.Enabled = false;
                    buttonReady2.Enabled = false;
                    buttonReady4.Enabled = false;

                    group_command.Move_T();
                    action3.Start();
                }
                else if (buttonStart3.Text == "停止")           //显示为“停止”    停止运动
                {
                    //group_command.Break();
                    buttonStart3.Text = "继续";
                    buttonReady3.Enabled = true;
                    buttonReady1.Enabled = true;
                    buttonReady2.Enabled = true;
                    buttonReady4.Enabled = true;
                    action3.Stop();
                }
            }
        }

        private void action3_tick(object sender, ElapsedEventArgs e)
        {
            if (this.frmain.group_counter_arm == 0)
            {
                this.frmain.group_counter_arm = 1;
            }
            else if (this.frmain.group_counter_arm == 1)
            {
                if (this.frmain.unstop_counter_arm == 0)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_T("02", "2C", "00", "30", "02", "2C", "00", "30", "02", "2C", "00", "30");
                }
                else if (this.frmain.unstop_counter_arm == 1)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_T("02", "D5", "00", "50", "02", "D5", "00", "50", "01", "83", "00", "50");
                }
                else if (this.frmain.unstop_counter_arm == 2)
                {
                    ++this.frmain.unstop_counter_arm;
                    group_command.AddPoint_T("02", "2C", "00", "30", "02", "2C", "00", "30", "02", "2C", "00", "30");
                }
                else if (this.frmain.unstop_counter_arm == 3)
                {
                    this.frmain.unstop_counter_arm = 0;
                    this.frmain.group_counter_arm = 2;
                    group_command.AddPoint_T("01", "83", "00", "50", "01", "83", "00", "50", "02", "D5", "00", "50");
                }
            }
            else if (this.frmain.group_counter_arm == 2)
            {
                this.frmain.group_counter_arm = 3;
            }
            else if (this.frmain.group_counter_arm == 3)
            {
                this.frmain.group_counter_arm = 3;
                group_command.BufferLeft_T("02");
            }
        }

        private void buttonReady4_Click(object sender, EventArgs e)
        {
            group_command.Start_T("01", "C4", "00", "20", "02", "2C", "00", "20", "01", "83", "00", "20", "02", "D5", "00", "20");

            flag_ready4 = true;
            buttonStart4.Enabled = true;
            buttonReady1.Enabled = false;
            buttonStart1.Enabled = false;
            buttonReady2.Enabled = false;
            buttonStart2.Enabled = false;
            buttonReady3.Enabled = false;
            buttonStart3.Enabled = false;
            buttonStart3.Text = "开始";
            buttonStart2.Text = "开始";
            buttonStart1.Text = "开始";
            buttonStart4.Text = "开始";
            //清除节点  00是位置时间  01是连续轨迹
            group_command.ClearBuffer("02", "00");
            group_command.ClearBuffer("03", "00");
            group_command.ClearBuffer("04", "00");
        }

        private void buttonStart4_Click(object sender, EventArgs e)
        {
            if (flag_ready4)
            {
                if (buttonStart4.Text == "开始" || buttonStart4.Text == "继续")   //显示为“开始”   开始运动
                {
                    if (buttonStart4.Text == "开始")
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 0;
                    }

                    buttonStart4.Text = "停止";
                    buttonReady4.Enabled = false;
                    buttonReady1.Enabled = false;
                    buttonReady2.Enabled = false;
                    buttonReady3.Enabled = false;

                    group_command.Move_T();
                    action4.Start();
                }
                else if (buttonStart4.Text == "停止")           //显示为“停止”    停止运动
                {
                    //group_command.Break();
                    buttonStart4.Text = "继续";
                    buttonReady4.Enabled = true;
                    buttonReady1.Enabled = true;
                    buttonReady2.Enabled = true;
                    buttonReady3.Enabled = true;
                    action4.Stop();
                }
            }
        }

        private void action4_tick(object sender, ElapsedEventArgs e)
        {
            if (this.frmain.group_counter_arm == 0)
            {
                this.frmain.group_counter_arm = 1;
            }
            else if (this.frmain.group_counter_arm == 1)
            {
                if (action4_count == 0)
                {
                    if (this.frmain.unstop_counter_arm == 0)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "2C", "00", "70", "02", "2C", "00", "70");
                    }
                    else if (this.frmain.unstop_counter_arm == 1)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "D5", "00", "40", "01", "83", "00", "40");
                    }
                    else if (this.frmain.unstop_counter_arm == 2)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "2C", "00", "50", "02", "2C", "00", "50");
                    }
                    else if (this.frmain.unstop_counter_arm == 3)
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 2;
                        group_command.AddPoint_T2("03", "04", "01", "83", "00", "60", "02", "D5", "00", "60");
                        action4_count = 1;
                    }
                }
                else if (action4_count == 1)
                {
                    if (this.frmain.unstop_counter_arm == 0)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "2C", "00", "30", "02", "2C", "00", "30");
                    }
                    else if (this.frmain.unstop_counter_arm == 1)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "D5", "00", "80", "01", "83", "00", "80");
                    }
                    else if (this.frmain.unstop_counter_arm == 2)
                    {
                        ++this.frmain.unstop_counter_arm;
                        group_command.AddPoint_T2("03", "04", "02", "2C", "00", "70", "02", "2C", "00", "70");
                    }
                    else if (this.frmain.unstop_counter_arm == 3)
                    {
                        this.frmain.unstop_counter_arm = 0;
                        this.frmain.group_counter_arm = 2;
                        group_command.AddPoint_T2("03", "04", "01", "83", "00", "40", "02", "D5", "00", "40");
                        action4_count = 0;
                    }
                }
            }
            else if (this.frmain.group_counter_arm == 2)
            {
                this.frmain.group_counter_arm = 3;
            }
            else if (this.frmain.group_counter_arm == 3)
            {
                this.frmain.group_counter_arm = 3;
                group_command.BufferLeft_T("03");
            }
        }

    }
}
